package cn.stylefeng.guns.modular.actualClineInformation.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
import cn.stylefeng.guns.modular.actualClineInformation.entity.QyuanPowerSdClineActual;
import cn.stylefeng.guns.modular.actualClineInformation.enums.QyuanPowerSdClineActualExceptionEnum;
import cn.stylefeng.guns.modular.actualClineInformation.mapper.QyuanPowerSdClineActualMapper;
import cn.stylefeng.guns.modular.actualClineInformation.pojo.request.QyuanPowerSdClineActualRequest;
import cn.stylefeng.guns.modular.actualClineInformation.service.QyuanPowerSdClineActualService;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Date;
import java.math.BigDecimal;

/**
 * 电网联络线实际信息表，记录每天96个时间点的电网运行预测数据业务实现层
 *
 * @author 我是谭先生🐻
 * @date 2025/03/14 13:47
 */
@Service
public class QyuanPowerSdClineActualServiceImpl extends ServiceImpl<QyuanPowerSdClineActualMapper, QyuanPowerSdClineActual> implements QyuanPowerSdClineActualService {

	@Override
    public void add(QyuanPowerSdClineActualRequest qyuanPowerSdClineActualRequest) {
        QyuanPowerSdClineActual qyuanPowerSdClineActual = new QyuanPowerSdClineActual();
        BeanUtil.copyProperties(qyuanPowerSdClineActualRequest, qyuanPowerSdClineActual);
        this.save(qyuanPowerSdClineActual);
    }

    @Override
    public void del(QyuanPowerSdClineActualRequest qyuanPowerSdClineActualRequest) {
        QyuanPowerSdClineActual qyuanPowerSdClineActual = this.queryQyuanPowerSdClineActual(qyuanPowerSdClineActualRequest);
        this.removeById(qyuanPowerSdClineActual.getId());
    }

    @Override
    public void edit(QyuanPowerSdClineActualRequest qyuanPowerSdClineActualRequest) {
        QyuanPowerSdClineActual qyuanPowerSdClineActual = this.queryQyuanPowerSdClineActual(qyuanPowerSdClineActualRequest);
        BeanUtil.copyProperties(qyuanPowerSdClineActualRequest, qyuanPowerSdClineActual);
        this.updateById(qyuanPowerSdClineActual);
    }

    @Override
    public QyuanPowerSdClineActual detail(QyuanPowerSdClineActualRequest qyuanPowerSdClineActualRequest) {
        return this.queryQyuanPowerSdClineActual(qyuanPowerSdClineActualRequest);
    }

    @Override
    public PageResult<QyuanPowerSdClineActual> findPage(QyuanPowerSdClineActualRequest qyuanPowerSdClineActualRequest) {
        LambdaQueryWrapper<QyuanPowerSdClineActual> wrapper = createWrapper(qyuanPowerSdClineActualRequest);
        Page<QyuanPowerSdClineActual> sysRolePage = this.page(PageFactory.defaultPage(), wrapper);
        return PageResultFactory.createPageResult(sysRolePage);
    }

    @Override
    public List<QyuanPowerSdClineActual> findList(QyuanPowerSdClineActualRequest qyuanPowerSdClineActualRequest) {
        LambdaQueryWrapper<QyuanPowerSdClineActual> wrapper = this.createWrapper(qyuanPowerSdClineActualRequest);
        return this.list(wrapper);
    }

    /**
     * 获取信息
     *
     * @author 我是谭先生🐻
     * @date 2025/03/14 13:47
     */
    private QyuanPowerSdClineActual queryQyuanPowerSdClineActual(QyuanPowerSdClineActualRequest qyuanPowerSdClineActualRequest) {
        QyuanPowerSdClineActual qyuanPowerSdClineActual = this.getById(qyuanPowerSdClineActualRequest.getId());
        if (ObjectUtil.isEmpty(qyuanPowerSdClineActual)) {
            throw new ServiceException(QyuanPowerSdClineActualExceptionEnum.QYUAN_POWER_SD_CLINE_ACTUAL_NOT_EXISTED);
        }
        return qyuanPowerSdClineActual;
    }

    /**
     * 创建查询wrapper
     *
     * @author 我是谭先生🐻
     * @date 2025/03/14 13:47
     */
    private LambdaQueryWrapper<QyuanPowerSdClineActual> createWrapper(QyuanPowerSdClineActualRequest qyuanPowerSdClineActualRequest) {
        LambdaQueryWrapper<QyuanPowerSdClineActual> queryWrapper = new LambdaQueryWrapper<>();

        Integer id = qyuanPowerSdClineActualRequest.getId();
        String usageDate = qyuanPowerSdClineActualRequest.getUsageDate();
        Integer timePoint = qyuanPowerSdClineActualRequest.getTimePoint();
        String timeSlot = qyuanPowerSdClineActualRequest.getTimeSlot();
        BigDecimal powerSupplyFromNorthchina = qyuanPowerSdClineActualRequest.getPowerSupplyFromNorthchina();
        BigDecimal powerSupplyFromYindong = qyuanPowerSdClineActualRequest.getPowerSupplyFromYindong();
        BigDecimal powerSupplyFromLugu = qyuanPowerSdClineActualRequest.getPowerSupplyFromLugu();
        BigDecimal powerSupplyFromZhaoyi = qyuanPowerSdClineActualRequest.getPowerSupplyFromZhaoyi();
        String createdAt = qyuanPowerSdClineActualRequest.getCreateTime();
        String updatedAt = qyuanPowerSdClineActualRequest.getUpdateTime();

        queryWrapper.eq(ObjectUtil.isNotNull(id), QyuanPowerSdClineActual::getId, id);
        queryWrapper.eq(ObjectUtil.isNotNull(usageDate), QyuanPowerSdClineActual::getUsageDate, usageDate);
        queryWrapper.eq(ObjectUtil.isNotNull(timePoint), QyuanPowerSdClineActual::getTimePoint, timePoint);
        queryWrapper.like(ObjectUtil.isNotEmpty(timeSlot), QyuanPowerSdClineActual::getTimeSlot, timeSlot);
        queryWrapper.eq(ObjectUtil.isNotNull(powerSupplyFromNorthchina), QyuanPowerSdClineActual::getPowerSupplyFromNorthchina, powerSupplyFromNorthchina);
        queryWrapper.eq(ObjectUtil.isNotNull(powerSupplyFromYindong), QyuanPowerSdClineActual::getPowerSupplyFromYindong, powerSupplyFromYindong);
        queryWrapper.eq(ObjectUtil.isNotNull(powerSupplyFromLugu), QyuanPowerSdClineActual::getPowerSupplyFromLugu, powerSupplyFromLugu);
        queryWrapper.eq(ObjectUtil.isNotNull(powerSupplyFromZhaoyi), QyuanPowerSdClineActual::getPowerSupplyFromZhaoyi, powerSupplyFromZhaoyi);
        queryWrapper.eq(ObjectUtil.isNotNull(createdAt), QyuanPowerSdClineActual::getCreateTime, createdAt);
        queryWrapper.eq(ObjectUtil.isNotNull(updatedAt), QyuanPowerSdClineActual::getUpdateTime, updatedAt);

        return queryWrapper;
    }

}